<?php


if (!function_exists("debugvar"))
{
    /**
     * 
     * returns svn nrevision number from revision.txt file, wich is ubdated by project commiting
     */
    function getSvnRevizionNumber()
    {
        $svn = File('revision.txt');
        $svnrev = $svn[0];
        unset($svn);
        return substr($svnrev
                    , 6
                    , (strlen(substr($svnrev,6)))-2);
    }
    function debugvar($var, $title = '')
    {
        if( (defined('SF_DEBUG') && SF_DEBUG) && (defined('SF_ENVIRONMENT') && (SF_ENVIRONMENT != 'prod')) )
        {
            ob_start();
            if( $title )
                echo "$title\n";
            print_r($var);
            $out = ob_get_clean();
            echo "<pre>";
            echo htmlentities($out);
            echo "</pre>";
        }
    }
    
    /**
     * Returns true if user has such role ID
     *
     * @param integer $roleId role ID from MyConstants::ROLE_NNN
     * @return boolean
     */
    function has_role_id($roleId)
    {
        $has = false;
        
        if( ($userId = get_user_id()) )
        {
            $user = User::getOrCreate($userId);
            if( preg_match("/\b" . (int)$roleId . "\b/", $user->getRoles()) )
            {
                $has = true;
            }
        }
        
        return $has;
    }
    

    function get_culture()
    {
        return sfContext::getInstance()->getUser()->getCulture();
    }

    function log_this($str, $logLevel = SF_LOG_INFO)
    {
        if( $str )
            sfContext::getInstance()->getLogger()->log($str, SF_LOG_INFO);
    }

   /**
     * Returns ID of logged in user or 0 if session timeout
     *
     * @return integer
     */
    function get_user_id() 
    {
        $user = sfContext::getInstance()->getUser();
        if( $user->isAuthenticated() )
            return (int)$user->getAttribute('userId');

        // Session timeout
        return 0;
    }
}


?>
